<?php
class Helper_ListCreator
{
	private $ListColumns;
	public $id;
	public $className;
	public $width;
	public $Column_Addable = true;
	public $DefaultValues;
	
	function __construct()
	{
		$this->ListColumns = array();
		$this->id = "";
		$this->className = "tableList";
		$this->width = "100%";
		$this->DefaultValues = array();
	}
	
	public function AddColumn( $paraColumn )
	{
		array_push($this->ListColumns, $paraColumn);
	}
	
	public function Create()
	{
		if( $this->id == ""){
			die('请设置列表的ID');
		}
		if( count($this->ListColumns) <=0)
		{
			die('必须为列表增加至少一列');
		}
		$strInsertRowScript = "";
		$strCheckValue = "";
		
		
		$strCheckValue = "function ".$this->id."DeleteRow()
			{
				var objTable = $(\"#" .$this->id. "tblGoods\")[0];
				
				var objCheckbox = $(\":checkbox:checked[name^='".$this->id."cbRow']\");
				
				var objValue=\"\";
				
				for( var i =  objCheckbox.length -1; i >=0; i--)
				{
					objValue = $(\"#" . $this->id . "tbValue\").val().split(',');
					$(\"#" . $this->id . "tbValue\").val(\"\");
						
					for( var j = 0; j<objValue.length; j++)
					{
						//alert(\"aaa\");
						if( objCheckbox[i].value != objValue[j])
						{
							if($(\"#" . $this->id . "tbValue\").val() == \"\")
							{
								$(\"#" . $this->id . "tbValue\").val( objValue[j] );
							} else {
								$(\"#" . $this->id . "tbValue\").val($(\"#" . $this->id . "tbValue\").val() + \",\" + objValue[j]);
							}
						}
					}
					$(objCheckbox[i]).parent().parent().remove();
				}
			}
			function PostValue( paraID )
			{
				var hashTable = new Object();
				var tempHash = new Object();
				var iNumber = $(\"#\" + paraID + \"tbValue\").val();
				if( iNumber == \"\"){
					//没有任何行
				} else if( iNumber.split(\",\").length == 1){
					hashTable = SingleValue(paraID, iNumber)
				} else {
					if(  iNumber.split(\",\").length != 1){
						iNumber = iNumber.split(\",\");
						for(var i=0; i< iNumber.length; i++){
							tempHash = SingleValue(paraID, iNumber[i]);
							for(var k in tempHash){
								hashTable[k] = tempHash[k];
							}
						}
					}
				}
				return hashTable;
			}
			function Validate( paraID )
			{
				var iNumber = $(\"#\"+paraID+\"tbValue\").val();
				//alert(iNumber);
				if( iNumber == \"\"){
					return true;//没有任何行
				} else{
					var arrNumber = iNumber.split(\",\");
					
					if( arrNumber.length == 1){
						return CheckValue(paraID, iNumber)
					} else {
						//alert(iNumber.length);
						for(var i=0; i<arrNumber.length; i++){
							//alert('第几行'+i);
							//alert(paraID);
							//alert(arrNumber[i]);
							if( CheckValue(paraID, arrNumber[i]) == false){
								return false;
								break;
							}
							//alert(i);
							//if( $.isFunction(validateListItem) == true){
							//	eval('validateListItem(\"'+paraID+'\", '+arrNumber[i]+')');
							//}
							//alert(i);
						}
					}
				}
			}
			function CheckValue(paraID, iNumber)
			{";
		
		$strRowPostValue = "
			function SingleValue(paraID, iNumber)
			{
				var hashTable = new Object();
				var objTable = $(\"#\"+paraID + \"tblGoods\")[0];
			";
		
		$strInsertRowScript = "
			function ".$this->id."InsertRow()
			{
				var objTable = $(\"#" .$this->id. "tblGoods\")[0];
				var iNumber = $(\"#" .$this->id. "tbValue\").val();
				if( iNumber == \"\")
				{
					iNumber = \"1\";
				} else {
					if(  iNumber.split(\",\").length != 1)
					{
						iNumber = iNumber.split(\",\");
						iNumber = parseInt(iNumber[iNumber.length - 1]) + 1;
					} else {
						iNumber = parseInt(iNumber) + 1;
					}
				}
				var objRow = objTable.insertRow( objTable.rows.length );
				objRow.className = \"childRow\";
				var objCell = objRow.insertCell(0);
				//objCell.className = \"Content\";
				objCell.innerHTML = \"<input type='checkbox' name='" .$this->id. "cbRow[]' id='" .$this->id. "cbRow\" + iNumber + \"' value='\" + iNumber + \"'>\";
			";
			
		$strTable = "
		<table id=\"".$this->id."tblGoods\" class=\"".$this->className."\" style=\"width:".$this->width.";\">";
		if( $this->Column_Addable == true){
		$strTable .= "<tr class=\"Header\">
			<td colspan=\"3\" class=\"childRow\">
				<img onclick=\"".$this->id."InsertRow()\" alt=\"Insert Row\" src=\"/image/button/rownew.gif\" />
				<img onclick=\"".$this->id."DeleteRow()\" alt=\"Delete Row\" src=\"/image/button/rowdelete.gif\" />";
		
				
		$strTable .= "	</td>
		</tr>";
		}
		$strTable .= "<tr class=\"header\">
			<td align=\"center\" nowrap=\"nowrap\" style=\"width: 3%; padding-right: 10px;\">
			<input id='".$this->id."tbValue' name='".$this->id."tbValue' style=\"display:none;\" /></td>";
		$i=-1;
		foreach( $this->ListColumns as $val){
			$i++;
			$val->_ControlEvent = array($val->_ControlEvent);
			//$val->_DropDownItems = array(
			//$val->_DropDownItems;
			///);
			
			//设置表格
			$strTable .= "
			<td nowrap=\"nowrap\" style=\"text-align:center;";

			if ($val->_Visible == false)
			{
				$strTable .= "display:none;";
			}
			if ($val->_Width != "")
			{
				$strTable .= "width:" . $val->_Width . ";";
			}
			$strTable .= "\">";
			
			if( $val->_ErrorFocus  == ""){
				$val->_ErrorFocus = $val->_Name;
				$val->_ErrorCaption = $val->_Caption;
			} else {
				$val->_ErrorCaption = $this->DoRetrieveErrorCaption($this->ListColumns, $val->_ErrorFocus);
			}
					
			if ($val->_IsRequired == true)
			{
				$strTable .= "<font color=\"red\">*</font>";

				//设置表格内容检查脚本
				$strCheckValue .= "if( $(\"#\"+paraID+\"" . $val->_Name . "\" + iNumber).val() == \"\")
				{
					$.showMessage(\"请在行\"+iNumber +\" " . $val->_ErrorCaption . "中输入内容\");
					$(\"#\"+paraID+\"" . $val->_ErrorFocus . "\" + iNumber).focus();
					return false;
				}";
			}
			
			if ($val->_TextType == "Numeric"
				|| $val->_TextType == "Int")
			{
				//echo($val->_TextType);
				//设置表格数字检查脚本
				$strCheckValue .= "
				//alert(paraID);
				if( $(\"#\"+paraID+\"" . $val->_Name . "\" + iNumber).val() != '' && isNaN($(\"#\"+paraID+\"" . $val->_Name . "\" + iNumber).val()) == true)
				{
					$.showMessage(\"请在" . $val->_ErrorCaption . "中输入数字\");
					$(\"#\"+paraID+\"". $val->_ErrorFocus . "\" + iNumber).focus();
					return false;
				}";
			}
			
			
			if ($val->_TextType == "Date")
			{
				//echo($val->_TextType);
				//设置表格数字检查脚本
				$strCheckValue .= "
				//alert(\"#\"+paraID+\"" . $val->_Name . "\" + iNumber);
				if( $.isDate($(\"#\"+paraID+\"" . $val->_Name . "\" + iNumber).val()) == false)
				{
					$.showMessage(\"请在" . $val->_ErrorCaption . "中输入日期\");
					$(\"#\"+paraID+\"". $val->_ErrorFocus . "\" + iNumber).focus();
					return false;
				}";
			}
			
			
			$strTable .= $val->_Caption . "</td>";

			//设置插入脚本
			$strInsertRowScript .= "
				objCell = objRow.insertCell(" . ($i + 1) . ");
				objCell.className = \"Content\";";
			
			
			if ($val->_Visible == false)
			{
				$strInsertRowScript .= "
				objCell.style.display = \"none\";";
			}
			$strRowPostValue .= "
				hashTable[paraID +\"" . $val->_Name . "\" + iNumber] = $(\"#\" + paraID +\"" . $val->_Name . "\" + iNumber).val();";
			if ($val->_ControlType == "Input"
				|| $val->_ControlType == "Label")
			{
				$strInsertRowScript .= "
				objCell.innerHTML = \"<input type='text' name='" . $this->id . $val->_Name . "[]' id='" . $this->id . $val->_Name . "\" + iNumber + \"'";
			} else if ($val->_ControlType == "DropDownList") {
				$strInsertRowScript .= "
				objCell.innerHTML = \"<select name='" . $this->id . $val->_Name . "[]' id='" . $this->id . $val->_Name . "\" + iNumber + \"'";
			}
			
			for ($ControlEventNumber = 0; $ControlEventNumber < $val->_ControlEvent.Length; $ControlEventNumber++)
			{
				$strInsertRowScript .= " " . $val->_ControlEvent[$ControlEventNumber] . "(\" + iNumber + \")' ";
			}

			$strInsertRowScript .= " style='";

			if ($val->_ControlType == "Label")
			{
				$strInsertRowScript .= "border:0px;";
			}
			if ($val->_ControlWidth == "")
			{
				$strInsertRowScript .= "width: 100%;";
			} else {
				$strInsertRowScript .= "width:" . $val->_ControlWidth . ";";
			}
			$strInsertRowScript .= "'";
			if ($val->_ControlType == "Label")
			{
				$strInsertRowScript .= " readonly='true'";
			}

			if ($val->_ControlType == "Input"
				|| $val->_ControlType == "Label")
			{
				if ($val->_DefaultValue != "")
				{
					$strInsertRowScript .= " Value='".$val->_DefaultValue."'";
				}
				$strInsertRowScript .= " />";
			} else if ($val->_ControlType =="DropDownList") {
				$strInsertRowScript .= ">";
				
				for ($j = 0; $j < count($val->_DropDownItems); $j++)
				{
					$values = explode("^", $val->_DropDownItems[$j]);
					//var_dump($values);
					if ($values[0] == $val->_DefaultValue)
					{
						$strInsertRowScript .= "<option Selected value=\\\"" .$values[0] . "\\\">" .$values[1] . "</option>";
					} else {
						$strInsertRowScript .= "<option value=\\\"" .$values[0] . "\\\">" .$values[1] . "</option>";
					}
				}
				$strInsertRowScript .= "</select>";
				
			}
			
			if ($val->_ReferImage != "")
			{
				$strInsertRowScript .= "<img src='" . $val->_ReferImage . "'";
			//	var_dump($val->_ReferEvent);
				for( $ReferEvent = 0; $ReferEvent< count($val->_ReferEvent); $ReferEvent++)
				{
					$strInsertRowScript .= " " . $val->_ReferEvent[$ReferEvent] . "(\" + iNumber + \")' ";
				}
				$strInsertRowScript .= " border='0' alt='Ref' align='absmiddle' hspace='1'>";
			}
			$strInsertRowScript .= "\";";
		}
		$strRowPostValue .= "
				return hashTable;
				}";
		$strTable .= "
		</tr>";
		
		
		
		
		
		
		
		
		//根据默认值，生成默认的数据列表
		$RowNum = 0; 
		foreach( $this->DefaultValues as $value){
			//设置表格
			$RowNum++;
			$strTable .= '<tr  class=\"childRow\">';
			//if( $this->Column_Addable == true){
				$strTable .= '<td class="Content"><input type="checkbox" /></td>';
			//}
			foreach( $this->ListColumns as $val){
				$val->_ControlEvent = array($val->_ControlEvent);
				
				$strTable .= '<td nowrap="nowrap" style="text-align:center;';
				if ($val->_Visible == false)
				{
					$strTable .='display:none;';
				}
				if ($val->_Width != "")
				{
					$strTable .= 'width:'. $val->_Width . ';';
				}
				$strTable .= '">';
				if ($val->_ControlType == "Input"
					|| $val->_ControlType == "Label")
				{
					$strTable .= '<input type="text" name="' . $this->id . $val->_Name .'[]" id="' . $this->id . $val->_Name . $RowNum .'"';
				}
				else if ($val->_ControlType == "DropDownList")
				{
					$strTable .= '<select name="' . $this->id . $val->_Name .'[]" id="' . $this->id . $val->_Name . $RowNum.'"';
				}
				
				for ($ControlEventNumber = 0; $ControlEventNumber < $val->_ControlEvent.Length; $ControlEventNumber++)
				{
					$strTable .= " " . $val->_ControlEvent[$ControlEventNumber] . "(\" + iNumber + \")' ";
				}
	
				$strTable .= ' style="';
	
				if ($val->_ControlType == "Label")
				{
					$strTable .= "border:0px;";
				}
				if ($val->_ControlWidth == "")
				{
					$strTable .= "width: 100%;";
				} else {
					$strTable .= "width:" . $val->_ControlWidth . ";";
				}
				$strTable .= '"';
				if ($val->_ControlType == "Label")
				{
					$strTable .= " readonly='true'";
				}
	
				if ($val->_ControlType == "Input"
					|| $val->_ControlType == "Label")
				{
					if ($value[$val->_Name] != "")
					{
						$strTable .= " value='".$value[$val->_Name]."'";
					}
					$strTable .= " />";
				} else if ($val->_ControlType =="DropDownList") {
					$strTable .= ">";
					
					for ($j = 0; $j < count($val->_DropDownItems); $j++)
					{
						$values = explode("^", $val->_DropDownItems[$j]);
						//var_dump($values);
						if ($values[0] == $value[$val->_Name])
						{
							$strTable .= '<option Selected value="' .$values[0] . '">' .$values[1] . '</option>';
						} else {
							$strTable .= '<option value="' .$values[0] . '">' .$values[1] . '</option>';
						}
					}
					$strTable .= "</select>";
				}
				
//				if ($val->_ReferImage != "")
//				{
//					$strTable .= "<img src='" . $val->_ReferImage . "'";
//				//	var_dump($val->_ReferEvent);
//					for( $ReferEvent = 0; $ReferEvent< count($val->_ReferEvent); $ReferEvent++)
//					{
//						$strTable .= " " . $val->_ReferEvent[$ReferEvent] . "(\" + iNumber + \")' ";
//					}
//					$strTable .= " border='0' align='absmiddle' hspace='1'>";
//				}
//				$strTable .= "\";";
			$strTable .= "</td>".chr(13) . chr(10);
			}
			$strTable .= "</tr>";
		}
		$strTable .= '<script type="text/javascript">
						$("#'.$this->id.'tbValue").val("';
		for($i=1; $i<=$RowNum; $i++){
			if($i==1){
				$strTable .= $i;
			} else {
				$strTable .= ','.$i;
			}
		}
		$strTable .= '");
		</script>';
		
		
		
		
		$strTable .= "</table>";
		
		echo $strTable;
		
		
		$strInsertRowScript .= "
				
				$(\"#".$this->id."Date\"+iNumber).datepicker({dateFormat: 'yy-mm-dd'});
				
				if($(\"#" . $this->id . "tbValue\").val() == \"\")
				{
					$(\"#" . $this->id . "tbValue\").val(String(iNumber));
				} else {
					$(\"#" . $this->id . "tbValue\").val($(\"#" . $this->id . "tbValue\").val() + \",\" + String(iNumber));
				}
			}
			";
		$strCheckValue .= "
			}
		";
		echo '
		<script type="text/javascript">'.$strInsertRowScript ."
		". $strCheckValue."
		".$strRowPostValue.'</script>';
		//DoResetValue(xValues);
	}
	
	public function DoRetrieveErrorCaption( $paraListColumns , $paraName)
	{
		$name = "";
		foreach($paraListColumns as $val ){
			if( $val->_Name == $paraName){
				$name = $val->_Caption;
				break;
			}
		}
		return $name;
	}
	public function DoResetValue($paraValue)
	{
		if ($paraValue != "")
		{
			$Rows = $sparaValue.Split('|');
			$strScript = "";
			for ($i = 0; $i < $Rows.Length - 1; $i++)
			{
				$strScript .= $this->id . "InsertRow();
					";
			}
			//$caption = ViewState["x"].ToString().Split(',');
			//$captionType = ViewState["y"].ToString().Split(',');
			$Columns = array();

			$iLos = 0;
			for ($i = 0; $i < $Rows.Length; $i++)
			{
				$iLos = 0;
				for ($j = 0; j < $caption.Length; $j++)
				{
					$Columns = $Rows[i].Split('^');

					$strScript .= "$(\"#".$this->id.$caption[j].ToString() . ($i + 1) . "\").val(\"" . $Columns[$j - $iLos] . "\");";
					$strScript .= "$(\"#".$this->id.$caption[j].ToString() . ($i + 1) . "\").attr(\"title\", \"" . $Columns[$j - $iLos] . "\");";
				}
			}

			echo '
			<script type="text/javascript">'.strScript.'</script>';
		}
	}
}
?>